<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@page import="java.net.URLDecoder"%>
<%@ page import="com.sz.common.SZUtility" %>
<%@page import="com.sz.business.bmo.IBusinessBMO"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String business_id = SZUtility.toString(request.getParameter("business_id"));
String param = SZUtility.toString(request.getParameter("param"));

String style1 = "font-size: 12px;";
String style2 = "width: 250px";

String name = "";
String address = "";
String tel1 = "";
String avg_fee = "";
String check_contact = "";
String check_tel = "";
String check_notice = "";
String cash_mode = "";

if(!business_id.equals("")) {
	Map map = new HashMap();
	map.put("BUSINESS_ID", business_id);
	ServletContext servletContext = request.getSession().getServletContext(); 
	ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	IBusinessBMO bmo = (IBusinessBMO)context.getBean("BusinessBMO");	
	List list = bmo.queryBusiness(map);
	if(list.size() > 0) {
		Map r = (Map)list.get(0);
		name			= SZUtility.toString(r.get("NAME"));
		address			= SZUtility.toString(r.get("ADDRESS"));
		tel1			= SZUtility.toString(r.get("TEL1"));
		avg_fee			= SZUtility.toString(r.get("AVG_FEE"));	
		check_contact	= SZUtility.toString(r.get("CHECK_CONTACT"));
		check_tel		= SZUtility.toString(r.get("CHECK_TEL"));
		check_notice 	= SZUtility.toString(r.get("CHECK_NOTICE"));
		cash_mode 		= SZUtility.toString(r.get("CASH_MODE"));			
	}
}

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	<link href="<%= request.getContextPath() %>/css/main.css" rel="stylesheet" type="text/css" />
		<base href="<%=basePath%>">

		<title>商家信息</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="business_summary">
		<meta http-equiv="description" content="订单详情" >
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
    	</style>
	</head>
	
	<body>
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/calendar.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
		
		<iframe align="left" width="260">
		
		</iframe>
		
		<!--  
	  	 <table width="100%" border="0" cellpadding="0" cellspacing="0">
	  	 
	  	 <tr>
	  	 	<th colspan="8" align="left">商家信息</th>	  	 	
	  	 </tr>
	  	 
	  	 <tr>	  	 	
	  	 	<td style="<%=style1%>">名称&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="name" readonly="readonly" value="<%=name%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">地址&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="address" readonly="readonly" value="<%=address%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">订餐电话&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="tel1" readonly="readonly" value="<%=tel1%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">人均消费&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="avg_fee" value="<%=avg_fee %>" readonly="readonly" />
	  	 	</td>
	  	 </tr>
	  	 <tr>	  	 	
	  	 	<td style="<%=style1%>">核单人&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="check_contact" readonly="readonly" value="<%=check_contact%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">核单人电话&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="check_tel" readonly="readonly" value="<%=check_tel%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">核单注意事项&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="check_notice" readonly="readonly" value="<%=check_notice%>" >
	  	 	</td>
	  	 	
	  	 	<td style="<%=style1%>">结算模式&nbsp;</td>
	  	 	<td>
	  	 		<input type="text" id="cash_mode" value="<%=cash_mode %>" readonly="readonly" />
	  	 	</td>
	  	 </tr>	  	  			 	
	  	</table>
	  	-->
	  	<br/>
	  	
	  	<table width="100%" border="0" cellpadding="0" cellspacing="0" >
	  		<tr>
	  			<td align="center">
	  				<input type="button" value="预占" onclick="onLockBusiness(1)" style="width: 80px">&nbsp;&nbsp;
	  				<input type="button" value="释放" onclick="onLockBusiness(0)" style="width: 80px">
	  			</td>
	  		</tr>
	  	</table>
		
		<br/>
		<div id="table_div" style= "width: 100%"></div>
		<div id="pager"></div>	
		
	
  	<SCRIPT LANGUAGE="JavaScript">
  	
  		var numPerPage = 10; //每页行数
		var paramValue = "<%=param%>";
		var methodCount = "queryTicketCount";
		var methodPage = "queryTicketPage";
		
		var table = new TableView('table_div');
		table.header = {
			TICKET_ID			: '订单编号',			
			CUST_TEL			: '客户电话',
			CUST_NAME			: '客户姓名',
			DEMAND_TIME_STR		: '预约时间',
			CHECK_B_FLAG_STR	: '商家核单',
			CHECK_C_FLAG_STR	: '客户核单',
			CHECK_B_OPERATE		: '商家',
			CHECK_C_OPERATE		: '客户'
		};		
		table.customDefine = {
			TICKET_ID			: 'style= "display: none;"',			
			CUST_TEL			: '',
			CUST_NAME			: '',
			DEMAND_TIME_STR		: '',
			CHECK_B_FLAG_STR	: '',
			CHECK_C_FLAG_STR	: '',
			CHECK_B_OPERATE		: '',
			CHECK_C_OPERATE		: ''			
		};
				
		table.dataKey = 'TICKET_ID';
		table.display.marker = false;
		table.display.multiple = false;
		table.display.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.display.multiple = false;
		table.pager.size = numPerPage;		

		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryTicket(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "TicketBMO.queryTicket",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});
  	  	}
  	  	
  	  	function onPagerClick(pageNO) {
  			queryTicket(pageNO, "FLASE");
  		}
  	  	
  	  	//刷新当前页
  		function refreshCurrentPage() {
  			queryTicket(pager.index, "FLASE");
  		}
  			
  		function onQueryTicket() {
  			if(paramValue != null && paramValue != "") {  		
	  			pager.index = 1;
	  	  		queryTicket(1, true);
  	  		}
  		}
  		
  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);
		   for(var i = 0; i < array.length; ++i) {		   		
		   		var url = "<input type=\"button\" value=\"核单\" onclick=\"onCheck_B('";
		   		url += array[i].TICKET_ID;
		   		url += "')\">";
		   		array[i].CHECK_B_OPERATE = url;
		   		
		   		var url = "<input type=\"button\" value=\"核单\" onclick=\"onCheck_C('";
		   		url += array[i].TICKET_ID;
		   		url += "')\">";
		   		array[i].CHECK_C_OPERATE = url;
		   		
		   		if(array[i].CHECK_B_FLAG == "1") array[i].CHECK_B_FLAG_STR="已消费";
		   		else if(array[i].CHECK_B_FLAG == "0") array[i].CHECK_B_FLAG_STR="待核单";
		   		else if(array[i].CHECK_B_FLAG == "-1") array[i].CHECK_B_FLAG_STR="未消费";
		   		
		   		if(array[i].CHECK_C_FLAG == "1") array[i].CHECK_C_FLAG_STR="已消费";
		   		else if(array[i].CHECK_C_FLAG == "0") array[i].CHECK_C_FLAG_STR="待核单";
		   		else if(array[i].CHECK_C_FLAG == "-1") array[i].CHECK_C_FLAG_STR="未消费";
		   		
		   }		  
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		}
 		function onCheck_B(ticket_id) {
 			var row = table.get(ticket_id);
 			if(row.CHECK_B_FLAG != "0") {
 				if(window.confirm("商家已经核单，确认要继续吗?") == false) return;
 			}
 			
 			var url = "<%=path%>";
  			url += "/ticket/checkTicket_Business.page?ticket_id=" + ticket_id;  						
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:700px";
			window.showModalDialog(url, window, param);
			queryTicketForUpdate(ticket_id);
 			
 		}
 		function onCheck_C(ticket_id) {
 			var row = table.get(ticket_id);
 			if(row.CHECK_C_FLAG != "0") {
 				if(window.confirm("客户已经核单，确认要继续吗?") == false) return;
 			}
 			
 			var url = "<%=path%>";
  			url += "/ticket/checkTicket_Custom.page?ticket_id=" + ticket_id;  						
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:700px";
			window.showModalDialog(url, window, param);
			queryTicketForUpdate(ticket_id);
 		}
 		
 		function queryTicketForUpdate(ticket_id) {
 			var param = "{";			
			param += " TICKET_ID:'" + ticket_id;
			param += "'";			
			param += "}"
			
  	  		var date = {
  	  			BEAN_METHOD 	: "TicketBMO.queryTicket",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: param,
  	    	  	METHOD_COUNT 	: "", //为空则不返回记录数
  	    	  	METHOD_PAGE 	: "" //为空则不分页  	    	  
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: updateTable,
				error: show_error
  	  		});
  	  	}
  	  	function updateTable(data) {
  	  		var jdata = eval("(" + data + ")"); 
		    var array = eval(jdata.LIST);
		    if(array.length == 0) return;
		    
		    var v = array[0];
		    //与query_success一致
		    var url = "<input type=\"button\" value=\"核单\" onclick=\"onCheck_B('";
	   		url += v.TICKET_ID;
	   		url += "')\">";
	   		v.CHECK_B_OPERATE = url;
	   		
	   		var url = "<input type=\"button\" value=\"核单\" onclick=\"onCheck_C('";
	   		url += v.TICKET_ID;
	   		url += "')\">";
	   		v.CHECK_C_OPERATE = url;
	   		
	   		if(v.CHECK_B_FLAG == "1") v.CHECK_B_FLAG_STR="已消费";
	   		else if(v.CHECK_B_FLAG == "0") v.CHECK_B_FLAG_STR="待核单";
	   		else if(v.CHECK_B_FLAG == "-1") v.CHECK_B_FLAG_STR="未消费";
	   		
	   		if(v.CHECK_C_FLAG == "1") v.CHECK_C_FLAG_STR="已消费";
	   		else if(v.CHECK_C_FLAG == "0") v.CHECK_C_FLAG_STR="待核单";
	   		else if(v.CHECK_C_FLAG == "-1") v.CHECK_C_FLAG_STR="未消费";
		  
		   		
		    var row = table.get(v.TICKET_ID);
		    if(row != null && row != false) {
		    	//row = v;  //直接赋值不行啊。。。
		    	//目前只能写死了
		    	
		    	row.CHECK_B_FLAG = v.CHECK_B_FLAG;
		    	row.CHECK_C_FLAG = v.CHECK_C_FLAG;
		    	row.CHECK_B_FLAG_STR = v.CHECK_B_FLAG_STR;
		    	row.CHECK_C_FLAG_STR = v.CHECK_C_FLAG_STR;
		    	table.render();
		    }
  	  	}
  	  	function onLockBusiness(lockflag) {
  	  		var date = {
  	  			OPERATE_TYPE	: "LOCK",
  	  			BUSINESS_ID 	: "<%=business_id%>",
  	  			LOCKFLAG		: lockflag
  	  			
  	  	    };
  	  	    
  	  		$.ajax({
				type: "POST",
				url: "lockBusiness.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: lock_success,
				error: show_error
  	  		});
  	  	}
  	  	   		
  		function lock_success(data) {
  			alert(data);
  		}
  		
  		onQueryTicket();
  	 </SCRIPT>
	
	</body>
</html>
